﻿--[[
   @brief    web 操作组件，前进、后退、刷新
   @author   hqzheng
   @version  1.0
   @date     2017-04-12
--]]



web_operation = {
    nav_panel_id = 0;
    url_list = nil;
    back_btn_id = 0;

    refresh_btn_id_ = 0;
    refresh_btn_enable_ = true;

    error_url = "";

    init = function (parent_id)
        ierd_tgp.tpf_ui.Require("common\\stack.lua");
        web_operation.nav_panel_id = parent_id;
        web_operation.back_btn_id = ui_utility.get_ctrl_id_by_path(parent_id, "btnPrev");
        web_operation.refresh_btn_id_ = ui_utility.get_ctrl_id_by_path(parent_id, "btnRefresh");
        ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "true");
        web_operation.url_list = Stack:new();
        comm_center.reg_service("SvrRefreshWGIFrame", "web_operation.on_refresh_wgiframe");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[web_operation] %s", msg), core_msg);
    end;

    show = function (is_show)
        web_operation.inner_log(string.format("[show]is_show:%s", tostring(is_show)));
        ui_utility.set_visible(web_operation.nav_panel_id, is_show);
    end;

    on_back_btn_click = function (uilib, control_id)
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "goback");
        web_operation.go_back();

        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "true");
            if web_operation.refresh_btn_enable_ == true then
                web_operation.refresh_btn_enable_ = false;
                ui_utility.setproperty_by_id(web_operation.refresh_btn_id_, "Disable", "true");
            end
            tpfui:AddInterval('web_operation.enable_web_btn', 1500);
        end;
    end;

    go_back = function ()
        local is_network_alive = comm_center.call_service("Svr_IsNetAlive", {});
        local url_info = web_operation.url_list:Pop();
        if nil ~= url_info then
            local callback_func = web_register_center.get_func_by_browser(
                "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
            if callback_func ~= nil and callback_func ~= "" then
                local rsp_data_table = 
                    web_utility.notify_event_comm_data("web_mainframe_comm_listen");
                if 0 == is_network_alive.alive then
                    -- 不可用则跳错误页面
                    url_info.url = LoadStr("WEGAME_FRAME_ERR_URL");
                end
                rsp_data_table.data.data = url_info;
                rsp_data_table.data.custom_cmd = "go_back";
                web_utility.lua_call_js_ex(callback_func, 
                    base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
            end
        end
        local size = web_operation.url_list:Size();
        if 0 == size then
            ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "true");
        end
    end;

    enable_web_btn = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            tpfui:RemoveInterval('web_operation.enable_web_btn');
        end;
        web_operation.update_back_btn_status();
        if web_operation.refresh_btn_enable_ == false then
            web_operation.refresh_btn_enable_ = true;
            ui_utility.setproperty_by_id(web_operation.refresh_btn_id_, "Disable", "false");
        end;
    end;

    on_refresh_wgiframe = function ()
        base_utility.log(string.format("[on_refresh_wgiframe] on_refresh_wgiframe"));
        web_operation.on_refresh_btn_click();
    end;

    on_refresh_btn_click = function (uilib, control_id)
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "refresh");

        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil and web_operation.refresh_btn_enable_ == true then
            web_operation.refresh_btn_enable_ = false;
            ui_utility.setproperty_by_id(web_operation.refresh_btn_id_, "Disable", "true");
            ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "true");
            tpfui:AddInterval('web_operation.enable_web_btn', 1500);
        end;

        local callback_func = web_register_center.get_func_by_browser(
            "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = 
                web_utility.notify_event_comm_data("web_mainframe_comm_listen");
            -- 增加web_st 和client st
            local web_st = "";
            local pkey_str = "";
            local login_mgr =
                 base_utility.get_component_adapter("ILogin_mgr",
                     "ierd_tgp.login_mgr.GetLoginMgr();");
            if login_mgr ~= nil then
                local info = ierd_tgp.login_mgr.WebStPod:new();
                login_mgr:GetWebSt(info);
                web_st = info.web_st;
                info:delete();
                local client_st_info = ierd_tgp.login_mgr.ClientStPod:new();
                login_mgr:GetClientSt(client_st_info);
                pkey_str = client_st_info.client_st;
                client_st_info:delete();
            end
            rsp_data_table.data.web_st = web_st;
            rsp_data_table.data.pkey = pkey_str;
            rsp_data_table.data.custom_cmd = "refresh_page";
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
        comm_center.call_service("srv_refresh_oss_cfg", {});
        if nil ~= wegame_mainframe_control and
           true == wegame_mainframe_control.mygame_list_ready then
            comm_center.call_service("Svr_UpdateSubscriptionList", {});
        end
    end;

    handle_refresh_page = function(json)
        web_operation.inner_log(string.format("handle_refresh_page:%s", tostring(json)), true);
        if json ~= nil then
            local param_table = base_utility.json_to_table(json);
            if param_table ~= nil and param_table.data ~= nil and 
               param_table.data.game_id ~= nil and param_table.data.url ~= nil then
                local info = {};
                info.module_id = param_table.data.game_id;
                info.module_url = param_table.data.url;
                if string.find(info.module_url, "loadFailed") ~= nil then
                    info.module_url = nil;
                end
                wegame_mainframe_model.jump_module_web_json = base_utility.table_to_json(info);
            end;
        end;

        local browser_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_QBWEB);
        if browser_id ~= 0 then
            web_register_center.handle_unregister_all_event("", "", browser_id);
        end
        local web_ptr = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_QBWEB);
        if web_ptr ~= nil then
            web_ptr:Refresh();
        end
    end;

    save_url_info = function (game_id, url)
        if nil == game_id or nil == url 
            or 0 == game_id or "" == url 
            or nil == web_operation.url_list then
            return;
        end

        if string.find(url, "loadFailed") ~= nil then
            return;
        end
        if string.find(url, "frames/error.html") ~= nil then
            return;
        end

        local top_url_info = web_operation.url_list:Top();
        if nil == top_url_info or game_id ~= top_url_info.game_id or url ~= top_url_info.url then
            local url_info = {};
            url_info["game_id"] = game_id;
            url_info["url"] = url;
            web_operation.url_list:Push(url_info);
            ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "false");
        end
    end;

    update_back_btn_status = function()
        local size = web_operation.url_list:Size();
        if size > 0 then
            ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "false");
        else
            ui_utility.setproperty_by_id(web_operation.back_btn_id, "Disable", "true");
        end
    end;
};
